<?php
include_once("$config[webroot]/lang/".$config['language']."/user_admin/global.php");
include_once("$config[webroot]/module/promo/lang/".$config['language']."/".$_GET['s'].".php");
include("module/promo/includes/plugin_promo_class.php");
include("module/promo/includes/promo_function.php");

if($_POST['pcatadd']){
    if(!empty($_POST['id'])){
        $sql="update ".PROMOCAT." set fid='".$_POST['fid']."',name='".$_POST['name']."' where id=".$_POST['id'];
        $db->query($sql);
        header("Location: ?action=m&m=promo&s=admin_promo_cat&menu=info");
    }else{
    $sql="insert into ".PROMOCAT."(fid,name,uid) values('$_POST[fid]','$_POST[name]','$buid')";
    $db->query($sql);
    header("Location: ?action=m&m=promo&s=admin_promo_cat&menu=info");
    }
}
if($_GET['id']){
    $sql="select * from ".PROMOCAT." where id =".$_GET['id']." and uid=".$buid;
    $db->query($sql);
    $upcat=$db->fetchRow();

}
if($_GET['did']){
    $sql="select * from ".PROMOCAT." where fid =".$_GET['did']." and uid=".$buid;
    $db->query($sql);
    if($db->fetchRow()){
        echo "<script type='text/javascript'>alert('该分类存在下级分类，不能删除！');history.go(-1);</script>";
    }else{
    $sql="delete from ".PROMOCAT." where id =".$_GET['did']." and uid=".$buid;
    $db->query($sql);
    header("Location: ?action=m&m=promo&s=admin_promo_cat&menu=info");
    }
}


$sql="select * from ".PROMOCAT." where fid =0 and uid=".$buid." order by id desc";
$db->query($sql);
$re=$db->getRows();
for($i=0;$i<count($re);$i++){
    $id=$re[$i]['id'];
    $sql="select * from ".PROMOCAT." where fid = ".$id." and uid=".$buid." order by id desc";
    $db->query($sql);
    $re[$i]['array']=$db->getRows();
}

$tpl->assign("upid",$upcat['id']);
$tpl->assign("upcat",$upcat);
$tpl->assign("catlist",$re);
$tpl->assign("lang",$lang);
$output = tplfetch("admin_promo_cat.htm");
?>
